package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleHomeQueryDto;
import com.heima.model.article.pojos.ApArticle;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ApArticleService
 *
 * @author liudo
 * @version 1.0
 * @project heima-leadnews
 * @date 2023/10/30 10:58:55
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    private static final String DYNA = "__dyna__";
    private static final String ALL = "__all__";

    @Override
    public List<ApArticle> load(ArticleHomeQueryDto articleHomeQueryDto) {
        // 对于 __dyna__ 和 __all__ 跳过频道查询 -- 动态SQL
        // 利用SQL查询返回结果集就可以
        // 如果项目中使用了MybatisPlus 且我们Mapper中第一个参数是 Page类型 就能实现分页查询
        Page<ApArticle> pageQuery = new Page<>(1, articleHomeQueryDto.getSize(), false);
        ApArticle apArticleQuery = new ApArticle();
        if (!StringUtils.equalsAny(articleHomeQueryDto.getTag(), DYNA, ALL)) {
            apArticleQuery.setChannelId(Integer.valueOf(articleHomeQueryDto.getTag()));
        }
        Page<ApArticle> page = baseMapper.selectPageLimit(pageQuery, apArticleQuery);
        return page.getRecords();
    }
}
